package com.ychs.dpmms.service;

import com.ychs.dpmms.dao.BranchMapper;
import com.ychs.dpmms.dao.DeveloperMapper;
import com.ychs.dpmms.dao.ReportMapper;
import com.ychs.dpmms.dao.UserMapper;
import com.ychs.dpmms.entity.Branch;
import com.ychs.dpmms.entity.Developer;
import com.ychs.dpmms.entity.Report;
import com.ychs.dpmms.entity.User;
import com.ychs.dpmms.util.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * copyright(c)2021 YYB.ALL rights Reserved
 * <p>
 * 描述:
 *
 * @author 原玉波
 * @version 1.0
 * @date 2021/8/30
 */
@Service
public class UserService implements IBaseService<User>{
    //slf4j日志对象
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    //引入mapper
    @Resource
    private UserMapper mapper;
    @Resource
    private DeveloperMapper developerMapper;
    @Resource
    private ReportMapper reportMapper;

    /**
     * 添加记录
     *
     * @param user 记录对象
     * @return 是否添加成功
     */
    @Override
    public boolean add(User user) {
        boolean result = mapper.insert(user) == Constants.UPDATE_SUCCESS;
        return result;
    }

    /**
     * 修改记录
     *
     * @param user 记录对象
     * @return 是否修改成功
     */
    @Override
    public boolean modify(User user) {
        boolean result = mapper.update(user) == Constants.UPDATE_SUCCESS;
        return result;
    }

    /**
     * 删除记录
     *
     * @param id 该记录的id
     * @return 是否删除成功
     */
    @Override
    public boolean remove(int id) {
        Map<String, Object> conditions = new HashMap<>();
        conditions.put("user", id);
        List<Developer> developerList = developerMapper.select(conditions);
        if (developerList.size() > 0) {
            for (int i = 0; i < developerList.size(); i++) {
                conditions = new HashMap<>();
                conditions.put("developer", developerList.get(i).getId());
                List<Report> reportList = reportMapper.select(conditions);
                if (reportList.size() > 0) {
                    for (int j = 0; j < reportList.size(); j++) {
                        reportMapper.delete(reportList.get(j).getId());
                    }
                }
                developerMapper.delete(developerList.get(i).getId());
            }
        }
        boolean result = mapper.delete(id) == Constants.UPDATE_SUCCESS;
        return result;
    }

    /**
     * 按照条件查询所有记录
     *
     * @param conditions 条件
     * @return 记录对象
     */
    @Override
    public List<User> search(Map<String, Object> conditions) {
        List<User> users = mapper.select(conditions);
        return users;
    }

    /**
     * 按照id查询记录
     *
     * @param id 记录的id
     * @return 记录对象
     */
    @Override
    public User searchById(int id) {
        User user = mapper.selectById(id);
        return user;
    }

    /**
     * 按照条件查询记录总数
     *
     * @param conditions 条件
     * @return 符合条件的记录总数
     */
    @Override
    public int searchTotalNumber(Map<String, Object> conditions) {
        int count = mapper.selectTotalNumber(conditions);
        return count;
    }

    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    public User login(String username,String password){
        User user = mapper.login(username, password);
        return user;
    }
}
